package com.macrounion.atv.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.macrounion.atv.processor.RegProcessor;
import com.macrounion.atv.service.entity.User;
import com.macrounion.base.config.BaseInterceptor;
import com.macrounion.base.service.exception.SessionException;
import com.macrounion.base.service.utils.SessionHandler;


/**
 * WebApi Interceptor
 * @author Administrator
 *
 */
@Component
public class WebApiInterceptor extends BaseInterceptor<User> {

	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpServletResponse response;
	@Autowired
	private RegProcessor regProcessor;

	
	@Override
	public boolean myPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if(!regProcessor.canUse()) {
			response.sendRedirect(request.getContextPath() +"/web/reg/index");
			return false;
		}
		User user = super.getSessionUser();
		if(user==null){
			if(request.getRequestURI().startsWith(request.getContextPath()+"/api/"))
				throw  new SessionException();
			response.sendRedirect(request.getContextPath() +"/login");
			return false;
		}
		SessionHandler.setContext(user);
		return true;
	}

	@Override
	protected void permissionHandle(String menuCode, List<String> actions) {
		//拦截获取用户信息
		//查询用户与menu关系表
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
//		RequestContext.removeContext();
	}

	@Override
	protected boolean hasMyHandle() {
		// TODO Auto-generated method stub
		return true;
	}

}
